package com.d3works.apps.account.service;

import com.d3works.apps.account.db.ReconcileGroup;
import com.d3works.apps.account.db.repo.ReconcileGroupRepository;
import com.d3works.apps.account.exception.AccountExceptionMessage;
import com.d3works.apps.base.db.repo.SequenceRepository;
import com.d3works.apps.base.service.administration.SequenceService;
import com.d3works.exception.D3worksException;
import com.d3works.exception.db.repo.TraceBackRepository;
import com.d3works.i18n.I18n;
import com.d3works.inject.Beans;

public class ReconcileGroupSequenceServiceImpl implements ReconcileGroupSequenceService {

  @Override
  public void fillCodeFromSequence(ReconcileGroup reconcileGroup) throws D3worksException {
    String sequenceCode;
    String exceptionMessage;
    if (reconcileGroup.getStatusSelect() == ReconcileGroupRepository.STATUS_FINAL) {
      sequenceCode = SequenceRepository.RECONCILE_GROUP_FINAL;
      exceptionMessage = AccountExceptionMessage.RECONCILE_GROUP_NO_FINAL_SEQUENCE;
    } else {
      sequenceCode = SequenceRepository.RECONCILE_GROUP_DRAFT;
      exceptionMessage = AccountExceptionMessage.RECONCILE_GROUP_NO_TEMP_SEQUENCE;
    }
    String code =
        Beans.get(SequenceService.class)
            .getSequenceNumber(
                sequenceCode, reconcileGroup.getCompany(), ReconcileGroup.class, "code");

    if (code == null) {
      throw new D3worksException(
          TraceBackRepository.CATEGORY_CONFIGURATION_ERROR,
          I18n.get(exceptionMessage),
          reconcileGroup);
    }

    reconcileGroup.setCode(code);
  }
}
